package com.yidu.controller;

import com.yidu.entity.DbUser;
import com.yidu.service.DbUserService;
import com.yidu.utils.BaseMessage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (DbUser)表控制层
 *
 * @author makejava
 * @since 2021-07-30 09:19:00
 */
@RestController
@RequestMapping("dbUser")
public class DbUserController {
    /**
     * 服务对象
     */
    @Resource
    private DbUserService dbUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public DbUser selectOne(Integer id) {
        return this.dbUserService.queryById(id);
    }

    @GetMapping("/findLogin")
    public BaseMessage findLogin(DbUser user){
        DbUser po = dbUserService.findLogin(user);
        System.out.println(po);
        if(po==null){
            return BaseMessage.NO("登录失败");
        }else {
            return BaseMessage.YES("登录成功",po);
        }
    }

    @PostMapping("/findLoginName")
    public BaseMessage findLoginName(String username,String userpwd){
        DbUser po = dbUserService.findLoginName(username, userpwd);
        System.out.println(po);
        if(po==null){
            return BaseMessage.NO("登录失败");
        }else {
            return BaseMessage.YES("登录成功",po);
        }
    }

    @PostMapping("/findByName")
    public BaseMessage findByName(String username){
        DbUser po = dbUserService.findByName(username);
        if(po==null){
            return BaseMessage.YES("该用户名可以使用");
        }else {
            return BaseMessage.NO("该用户名已存在");
        }
    }

    @PostMapping("/add")
    public BaseMessage insert(DbUser user){
        DbUser po = dbUserService.insert(user);
        return BaseMessage.YES("注册成功",po);
    }

    @GetMapping("/list")
    public Map<String,Object> list(DbUser vo){
        List<DbUser> list = this.dbUserService.queryAllByLimit(vo);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",10);
        map.put("data",list);
        return map;
    }

}
